<?php
include_once 'Zend/Db/Table.php';
include_once 'Merchant.php';
include_once 'Program.php';
Zend_Db_Table::setDefaultAdapter($db);

class Account extends Zend_Db_Table 
{
	protected $_name = 'affiliSt_accounts';
	
	/**
	 * add new user
	 *
	 * @param array
	 * @return boolean
	 */
	public function add($newUser)
	{
		try 
		{
			if ($userId = $this->insert($newUser))
			{
				$newMerchant['merchant_id'] = $userId;
				$newMerchant['merchant_firstname'] = $newUser['accFirstName'];
				$newMerchant['merchant_lastname'] = $newUser['accLastName'];
				$newMerchant['merchant_company'] = $newUser['accFirstName'].' '.$newUser['accLastName'];
				$newMerchant['merchant_address'] = $newUser['accBusinessAddressLine1'];
				$newMerchant['merchant_city'] = $newUser['accCity'];
				$newMerchant['merchant_country'] = $newUser['accCountry'];
				$newMerchant['merchant_phone'] = $newUser['accContactPhone'];
				$newMerchant['merchant_url'] = $newUser['accWebsiteUrl'];
				$newMerchant['merchant_category'] = 'software';
				$newMerchant['merchant_status'] = 'approved';
				$newMerchant['merchant_date'] = $newUser['accCreated'];
				$newMerchant['merchant_type'] = 'normal';
				$newMerchant['merchant_currency'] = 'Dollar';
				$newMerchant['merchant_zip'] = $newUser['accZipCode'];
				$newMerchant['merchant_isInvoice'] = 'Yes';
				$newMerchant['merchant_invoiceStatus'] = 'active';
				$newMerchant['merchant_pgmapproval'] = 'automatic';
				$merchant = new Merchant();
				$merchant->add($newMerchant);
				
				$newLogin['login_email'] = $newUser['accContactEmail'];
				$newLogin['login_password'] = '123';
				$newLogin['login_flag'] = 'm';
				$newLogin['login_id'] = $userId;
				$newLogin['login_retry_limit'] = 10;
				$merchant->addLogin($newLogin);
				
				$program = new Program();
				$newProgram['program_merchantid'] = $userId;
				$newProgram['program_url'] = $newUser['accWebsiteUrl'];
				$newProgram['program_description'] = 'This program is owned by '.$newUser['accFirstName'].' '.$newUser['accLastName'];
				$newProgram['program_ipblocking'] = 1;
				$newProgram['program_cookie'] = 10;
				$newProgram['program_date'] = $newUser['accCreated'];
				$newProgram['program_status'] = 'active';
				$newProgram['program_fee'] = 10;
				$newProgram['program_type'] = 0;
				$newProgram['program_value'] = 10;
				$newProgram['program_countries'] = $newUser['accCountry'];
				$newProgram['program_geotargeting_click'] = 1;
				$newProgram['program_geotargeting_lead'] = 1;
				$newProgram['program_geotargeting_sale'] = 1;
				$newProgram['program_geotargeting_impression'] = 1;
				$program->add($newProgram);
				
				return true;
			}
		}
		catch (Exception $e)
		{
			return false;
		}
	}
	public function edit($editUser, $username)
	{
		try 
		{
			if ($this->update($editUser,"accUsername = '$username'"))
			{
				return true;
			}
		}catch (Exception $e)
		{
			return false;
		}
	}
	public function get($accUsername)
	{
		$currUser = $this->fetchRow("accUsername = '$accUsername'");
		return $currUser->toArray();
	}
	public function getId($accUsername)
	{
		$currUser = $this->fetchRow("accUsername = '$accUsername'");
		$currUser = $currUser->toArray();
		return $currUser['accId'];
	}
	public function getAll()
	{
		$allUser = $this->fetchAll();
		return $allUser->toArray();
	}
	public function getById($accId)
	{
		$currUser = $this->fetchRow("accId = '$accId'");
		return $currUser->toArray();
	}
	public function getByEmail($accContactEmail)
	{
		if ($currUser = $this->fetchRow("accContactEmail = '$accContactEmail'"))
		{
			return $currUser->toArray();
		}
		return false;
	}
}
?>